﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace JsonXmlMutualTransfer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileName = string.Empty;
        private void btnImportXML_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Xml文件(*.xml,*.xsd)|*.xml;*.xsd";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog.FileName;
            }
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }
            using (StreamReader sr = new StreamReader(fileName))
            {
                string xmlContent = sr.ReadToEnd();
                this.txtContent.Text = xmlContent;
            }
        }

        private void btnSaveJsonFile_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(fileName);

            string jsonText = JsonConvert.SerializeObject(doc, Newtonsoft.Json.Formatting.Indented);
            string baseTaskDir = AppDomain.CurrentDomain.BaseDirectory + @"Struct";
            if (!Directory.Exists(baseTaskDir))
            {
                Directory.CreateDirectory(baseTaskDir);
            }

            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            string fileDir = baseTaskDir + @"\" + "json文件demo.json";
            using (FileStream fs = new FileStream(fileDir, FileMode.OpenOrCreate))
            {
                StreamWriter wr = null;
                wr = new StreamWriter(fs);
                wr.WriteLine(jsonText);
                wr.Flush();
            }



            //XmlDocument doc = new XmlDocument();
            //doc.Load(fileName);

            // 序列化这个对象
            //int obj = 1;
            //XmlSerializer serializer = new XmlSerializer(doc.GetType());

            //// 将对象序列化输出到文件
            //FileStream stream = new FileStream("hh.xml", FileMode.Create);

            //XmlWriterSettings settings = new XmlWriterSettings();
            //settings.Indent = true;
            //settings.IndentChars = "    ";
            //settings.NewLineChars = "\r\n";
            //settings.Encoding = Encoding.UTF8;
            //settings.OmitXmlDeclaration = true;  // 不生成声明头

            //using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
            //{
            //    // 强制指定命名空间，覆盖默认的命名空间
            //    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            //    namespaces.Add(string.Empty, string.Empty);
            //    serializer.Serialize(xmlWriter, doc);
            //    xmlWriter.Close();
            //};
            //stream.Close();

        }

        private void btnJsonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Json文件(*.json)|*.json";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                fileName = openFileDialog.FileName;
            }
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }
            using (StreamReader sr = new StreamReader(fileName))
            {
                string jsonContent = sr.ReadToEnd();
                this.txtContent.Text = jsonContent;
            }
        }

        private void btnSaveXmlFile_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            XmlDocument doc = null;
            using (StreamReader sr = new StreamReader(fileName))
            {
                string jsonContent = sr.ReadToEnd();
                doc = JsonConvert.DeserializeXmlNode(jsonContent);
            }

            string baseTaskDir = AppDomain.CurrentDomain.BaseDirectory + @"Struct";
            if (!Directory.Exists(baseTaskDir))
            {
                Directory.CreateDirectory(baseTaskDir);
            }

            if (string.IsNullOrEmpty(fileName) || doc == null)
            {
                return;
            }

            StringWriter sw = new StringWriter();
            using (XmlTextWriter writer = new XmlTextWriter(sw))
            {
                writer.Indentation = 2;  // the Indentation
                writer.Formatting = System.Xml.Formatting.Indented;
                doc.WriteContentTo(writer);
                writer.Close();
            }

            string fileDir = baseTaskDir + @"\" + "xml文件demo.xml";
            using (FileStream fs = new FileStream(fileDir, FileMode.OpenOrCreate))
            {
                StreamWriter wr = null;
                wr = new StreamWriter(fs);
                wr.WriteLine(sw.ToString());
                wr.Flush();
            }

        }
    }
}
